﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.标签打印;
using DXApplicationTangche.UC.流程.Service;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.项目成果管理.在制品管理.service;
using DXApplicationTangche.service;
using mendian;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.订单.service;
using DXApplicationTangche.UC.流程;
using DXApplicationTangche.UC.门店出入库.Service;
using DXApplicationTangche.UC.成品出库单.service;
using DXApplicationTangche.UC.下单MTO;
using DXApplicationTangche.UC.套号管理.套号下单;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.model;
using DXApplicationTangche.UC.订单一览新.订单预览新;
using static DXApplicationTangche.UC.ImpUC.XtraUserControl订单一览;
using static DXApplicationTangche.UC.门店下单.form.Frm订单一览;
using DXApplicationTangche.UC.样品管理.样品订单预览;

namespace DXApplicationTangche.UC.订单
{
    public partial class Form返修接收 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private String barcode;
        private OrderCommitDTO返回 orderCommitDTO返回;
        private CustomerPDTO customerPDTO = new CustomerPDTO();
        private ProcessInstanceRecordDTO processInstanceRecord;
        private String orderId = "";
        public event AddTabDataEvents addTabDataEvents;//定义事件
        private Enum订单类别 enum订单类别 = Enum订单类别.销售订单;

        public Form返修接收()
        {
            InitializeComponent();
            this.barButtonItem确认返修接收.Enabled = false;
            this.barButtonItem启动返修流程.Enabled = false;
        }
        private void Form返修接收_Load(object sender, EventArgs e)
        {
            this.ActiveControl = this.textBox成衣码;

        }
        private void textBox成衣码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.textBox成衣码.Text.Trim()))
                {
                    return;
                }
                this.barcode = this.textBox成衣码.Text.Trim();
                this.查询();
            }
        }
        private void 查询()
        {
            try
            {
                if (!String.IsNullOrEmpty(this.barcode))
                {
                    this.processInstanceRecord = 流程Service.getBarcodeStatus(this.barcode);
                }
                else
                {
                    this.processInstanceRecord = new ProcessInstanceRecordDTO();
                }
            }
            catch
            {
                MessageBox.Show("无查询信息");
                this.textBox成衣码.Clear();
                this.ActiveControl = this.textBox成衣码;
                this.processInstanceRecord = new ProcessInstanceRecordDTO();
                this.barcode = null;
            }
            //  判断条码是否为报废条码
            if (this.processInstanceRecord.deprecated)
            {
                MessageBox.Show("条码为报废条码");
                this.textBox成衣码.Clear();
                this.ActiveControl = this.textBox成衣码;
                this.processInstanceRecord = new ProcessInstanceRecordDTO();
                this.barcode = null;
            }
            this.显示();
            this.设置();
        }
        private void 显示()
        {
            //  订单信息
            this.orderCommitDTO返回 = this.processInstanceRecord.orderCommitDto;
            this.orderId = this.orderCommitDTO返回.orderId;
            this.textBox订单号.Text = this.orderCommitDTO返回.orderNo;
            this.textBox订单时间.Text = this.orderCommitDTO返回.orderDate.ToString();
            this.textBox款式.Text = this.orderCommitDTO返回.styleNameCn;
            try { this.textBox尺码.Text = this.orderCommitDTO返回.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { this.textBox尺码.Text = ""; }
            this.textBox面料名称.Text = this.orderCommitDTO返回.materialNameCn;
            this.textBox面料编码.Text = this.orderCommitDTO返回.materialCode;
            this.richTextBox备注.Text = this.orderCommitDTO返回.remarks;

            //比对订单客户信息和客户地址信息
            this.customerPDTO = MTO订单Service.getCustomerPByMobile(this.orderCommitDTO返回.mobile);
            this.textBox订单客户信息.Text =
                this.orderCommitDTO返回.customerName + "|"
                + this.orderCommitDTO返回.mobile + "|"
                + this.orderCommitDTO返回.addressProvince
                + this.orderCommitDTO返回.addressCity
                + this.orderCommitDTO返回.addressDistrict
                + this.orderCommitDTO返回.addressDetail;
            this.textBox客户地址信息.Text =
                this.customerPDTO.customerName + "|"
                + this.customerPDTO.mobile + "|"
                + this.customerPDTO.addressProvince
                + this.customerPDTO.addressCity
                + this.customerPDTO.addressDistrict
                + this.customerPDTO.addressDetail;
            try
            {
                //this.pictureBox款式.LoadAsync(URL.图片url + URL.款式图片路径 + this.qcBarcodeItemsDTO.styleImagePath);
                //this.pictureBox面料.LoadAsync(URL.图片url + URL.面料图片路径 + this.qcBarcodeItemsDTO.materialImagePath);
                this.pictureBox款式.Image =
                    FileService.GetImageByUrl(
                            URL.图片url + URL.款式图片路径 + RestCall.httpGetMethod(
                                URL.接口url + "/image/getStyleImagePathByStyleId?styleId=" + this.orderCommitDTO返回.refStyleId
                                )
                            );
                this.pictureBox面料.Image
                    = FileService.GetImageByUrl(
                        URL.图片url + URL.面料图片路径 + RestCall.httpGetMethod(
                            URL.接口url + "/image/getImagePath?materialCode=" + this.orderCommitDTO返回.materialCode
                            )
                        );
            }
            catch
            {
                this.pictureBox款式.Image = FunctionHelper.getSSHIRT();
                this.pictureBox面料.Image = FunctionHelper.getSSHIRT();
            }
            //  流程信息
            this.label订单类型.Text = "订单类型：" + this.processInstanceRecord.barcodeStatus;
            this.订单状态(this.processInstanceRecord.orderStatus);
            //
            this.textBox客户名.Text = this.processInstanceRecord.customerName;
            this.textBox收件人.Text = this.processInstanceRecord.consignee;
            this.textBox电话.Text = this.processInstanceRecord.mobile;
            this.textBox地址.Text = this.processInstanceRecord.addressProvince
                                + this.processInstanceRecord.addressCity
                                + this.processInstanceRecord.addressDistrict
                                + this.processInstanceRecord.addressDetail;
            //  能查到订单信息但流程未开始时，才提示流程未开始
            if (String.IsNullOrEmpty(this.processInstanceRecord.status))
            {
                this.label返修流程状态.ForeColor = Color.Red;
                this.label返修流程状态.Text = "返修流程状态：未启动";
                this.textBox返修流程发起时间.Text = "";
                MessageBox.Show("未启动返修流程");
                this.barButtonItem启动返修流程.Enabled = true;
            }
            else
            {
                this.label返修流程状态.ForeColor = Color.Blue;
                this.label返修流程状态.Text = "返修流程状态：" + this.processInstanceRecord.status;
                this.textBox返修流程发起时间.Text = this.processInstanceRecord.startTime.ToString();
                this.barButtonItem启动返修流程.Enabled = false;
            }
        }
        private void 设置()
        {
            //  确认接收按钮
            if (!String.IsNullOrEmpty(this.processInstanceRecord.id))
            {
                //  返修流程未完成且订单存在未取消时，才执行跳转完成节点 且成衣码未报废
                if (ProcessStatusConsts.ON_GOING.Equals(this.processInstanceRecord.status) 
                    && !Const订单状态.CANCEL.Equals(this.processInstanceRecord.orderStatus) 
                    && !Const订单状态.INVALID.Equals(this.processInstanceRecord.orderStatus)
                    && !this.processInstanceRecord.deprecated
                    )
                {
                    this.barButtonItem确认返修接收.Enabled = true;
                }
                else
                {
                    this.barButtonItem确认返修接收.Enabled = false;
                }
            }
            else
            {
                this.barButtonItem确认返修接收.Enabled = false;
            }
        }
        private void 订单状态(String orderStatus)
        {
            if (Const订单状态.NOT_SCHEDULED.Equals(orderStatus))
            {
                this.label订单状态.Text = "订单状态：未排产";
            }
            else if (Const订单状态.SCHEDULED.Equals(orderStatus))
            {
                this.label订单状态.Text = "订单状态：已排产";
            }
            else if (Const订单状态.WIP.Equals(orderStatus))
            {
                this.label订单状态.Text = "订单状态：在制品";
            }
            else if (Const订单状态.DONE.Equals(orderStatus))
            {
                this.label订单状态.Text = "订单状态：完成";
            }
            else if (Const订单状态.CANCEL.Equals(orderStatus))
            {
                this.label订单状态.Text = "订单状态：取消";
            }
            else if (Const订单状态.INVALID.Equals(orderStatus))
            {
                this.label订单状态.Text = "订单状态：不存在";
            }
            else
            {
                this.label订单状态.Text = "订单状态：";
            }
        }

        #region 按钮
        private void simpleButton置空_Click(object sender, EventArgs e)
        {
            this.textBox成衣码.Clear();
            this.ActiveControl = this.textBox成衣码;
            this.barcode = null;
            this.查询();
        }
        
        private void barButtonItem确认返修接收_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!String.IsNullOrEmpty(this.barcode))
            {
                Form返修接收确认 form = new Form返修接收确认(this.barcode);
                form.ShowDialog();
                if (form.isDone)
                {
                    this.barButtonItem确认返修接收.Enabled = false;
                    this.查询();
                }
                else
                {
                    this.barButtonItem确认返修接收.Enabled = true;
                }
            }
        }

        private void barButtonItem启动返修流程_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!String.IsNullOrEmpty(this.barcode))
            {
                new Form新建返修(this.barcode).ShowDialog();
                this.查询();
            }
            else
            {
                MessageBox.Show("无成衣码");
            }
        }

        private void barButtonItem打印水洗标_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!String.IsNullOrEmpty(this.barcode))
            {
                //ture 为定制水洗，false为套号水洗
                if (流程Service.isCustomerMade(this.barcode))
                {
                    new Form水洗标(this.barcode).ShowDialog();
                }
                else {
                    new Form套号水洗标(this.barcode).ShowDialog();
                }
            }
            else
            {
                MessageBox.Show("无成衣码");
            }
        }
        
        private void barButtonItem条码作废_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!String.IsNullOrEmpty(this.barcode))
            {
                new Form条码报废处置(this.barcode).ShowDialog();
                //this.查询();
            }
            else
            {
                MessageBox.Show("无成衣码");
            }
        }
        private void barButtonItem打印订单纸_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!String.IsNullOrWhiteSpace(this.orderId))
            {
                //  全局变量赋值            
                this.orderId = this.orderCommitDTO返回.orderId;
                if (this.enum订单类别 == Enum订单类别.销售订单)
                {
                    new Form订单预览新(this.orderId).ShowDialog();
                }
                else if (this.enum订单类别 == Enum订单类别.样品订单)
                {
                    new Form样品订单预览新(this.orderId).ShowDialog();
                }
                else
                {
                    new Form订单预览新(this.orderId).ShowDialog();
                }
            }
            else
            {
                MessageBox.Show("成衣条码有误");
                return;
            }
        }
        #endregion

    }
}